package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.BaseUtil;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.FollowCollect;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的收藏适配器
 *
 * @author GuiyouZhang
 *         Date：2016年1月7日 上午9:46:05
 *         Email:guiyouzhang@163.com
 */
public class CollectAdapter extends BaseAdapter {

    private Context mContext;
    public ArrayList<FollowCollect> mDatas;
    private OnItemClickListener clickListener;
    private int type = 1;//1实物类商品，2预约类商品
    private boolean isEdit = false;

    public CollectAdapter(Context mContext, ArrayList<FollowCollect> collects, int type) {
        super(mContext);
        this.mDatas = collects;
        this.mContext = mContext;
        this.type = type;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return mDatas.size() == 0 ? 1 : mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        if (type == 1) {
            ViewHolder1 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_collect_production, null);
                holder = new ViewHolder1(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else {
                holder = (ViewHolder1) convertView.getTag(R.id.TAG_VIEWHOLDER);
            }
            holder.setDatas(position);
            return convertView;
        }else {
            ViewHolder2 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_collect_book_production, null);
                holder = new ViewHolder2(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            }else {
                holder = (ViewHolder2) convertView.getTag(R.id.TAG_VIEWHOLDER);
            }
            holder.setDatas(position);
            return convertView;
        }
    }

    class ViewHolder1 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_selector)
        ImageView ivSelector;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sales)
        TextView tvSales;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_price_old)
        TextView tvPriceOld;
        @Bind(R.id.tv_size)
        TextView tvSize;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        @Bind(R.id.iv_shopping_cart)
        ImageView ivShoppingCart;
        int position = -1;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            FollowCollect blog = mDatas.get(position);
            ImageUtils.loadRadiusImage(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getName());
            tvSales.setText("已售："+blog.getSellcount());
            tvPrice.setText("¥"+blog.getPrice());
            tvPriceOld.setText("¥"+blog.getOldprice());
            tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
//            tvSize.setText("规格:1"+blog.getUnit());
            tvSize.setVisibility(View.GONE);

            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            if (price-score < 0) {
                tvCash.setText("¥0.00"+"+");
            }else {
                tvCash.setText("¥"+uuUtils.formatAfterDot2(price-score+"")+"+");
            }
            tvFoodStamp.setText(blog.getScore()+"粮票");
            if (isEdit) {
                ivSelector.setVisibility(View.VISIBLE);
                if (blog.isCheck())
                    ivSelector.setImageResource(R.drawable.select_y_orange);
                else
                    ivSelector.setImageResource(R.drawable.select_n);
            }else
                ivSelector.setVisibility(View.GONE);
        }
    }

    class ViewHolder2 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_selector)
        ImageView ivSelector;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_sales)
        TextView tvSales;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_price_old)
        TextView tvPriceOld;
        @Bind(R.id.tv_distance)
        TextView tvDistance;
        @Bind(R.id.tv_cash)
        TextView tvCash;
        @Bind(R.id.tv_food_stamp)
        TextView tvFoodStamp;
        int position = -1;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            FollowCollect blog = mDatas.get(position);
            ImageUtils.loadRadiusImage(mContext, blog.getImgurl(), ivBkg, 10);
            tvName.setText(blog.getName());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(blog.getPrice()));
            tvPriceOld.setText("¥" + uuUtils.formatAfterDot2(blog.getOldprice()));
            tvPriceOld.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线
            tvSales.setText("已售："+blog.getSellcount());
            if (!isNull(BaseUtil.transNull(blog.getDistance()))) {
                tvDistance.setVisibility(View.VISIBLE);
                tvDistance.setText(blog.getDistance() + "km");
            }else {
                tvDistance.setVisibility(View.GONE);
            }
            tvFoodStamp.setText(uuUtils.formatAfterDot2(blog.getScore()));

            double price = Double.parseDouble(blog.getPrice());
            double score = Double.parseDouble(blog.getScore());
            if (price-score < 0) {
                tvCash.setText("¥0.00"+"+");
            }else {
                tvCash.setText("¥"+uuUtils.formatAfterDot2(price-score+"")+"+");
            }
            tvFoodStamp.setText(blog.getScore()+"粮票");
            if (isEdit) {
                ivSelector.setVisibility(View.VISIBLE);
                if (blog.isCheck())
                    ivSelector.setImageResource(R.drawable.select_y_orange);
                else
                    ivSelector.setImageResource(R.drawable.select_n);
            }else
                ivSelector.setVisibility(View.GONE);
        }
    }
}
